package com.ruoyi.law.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageCondition;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.law.domain.entity.LawBanner;
import com.ruoyi.law.service.ILawBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author code
 * @since 2022-12-08
 */
@Validated
@Api(tags = "轮播图")
@RestController
@RequestMapping("/banner")
public class LawBannerController extends BaseController {

    @Autowired
    private ILawBannerService bannerService;

    @Anonymous
    @ApiOperation("列表")
    @GetMapping("/list")
    public R<Page<LawBanner>> list(PageCondition<LawBanner> pageCondition) {
        Page<LawBanner> page = bannerService.page(pageCondition.toPage("sort", false));
        return R.ok(page);
    }

    @Anonymous
    @ApiOperation("详情")
    @GetMapping("/info")
    public R<LawBanner> info(Long id) {
        LawBanner lawBanner = bannerService.getById(id);
        return R.ok(lawBanner);
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    public R<Object> save(@Validated @RequestBody LawBanner lawBanner) {
        LambdaQueryWrapper<LawBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawBanner::getBannerType, lawBanner.getBannerType());
        int count = bannerService.count(queryWrapper);
        if (count >= 10) {
            return R.fail("最多只能添加10个轮播图");
        }
        bannerService.save(lawBanner);
        return R.ok();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody LawBanner lawBanner) {
        bannerService.updateById(lawBanner);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long id) {
        boolean removeById = bannerService.removeById(id);
        return removeById ? R.ok("删除成功") : R.fail("删除失败");
    }

    @ApiOperation("通过类型查询要跳转的页面信息")
    @ApiImplicitParam(name = "type", value = "跳转类型(1.新闻，2.咨询，3.服务，4.律师详情)",
            required = true, dataType = "String")
    @GetMapping("/getByType")
    public R<List<Map<String, Object>>> getByType(String type) {
        List<Map<String, Object>> res = bannerService.getByType(type);
        return R.ok(res);
    }

    @Anonymous
    @ApiOperation("通过类型查询轮播图")
    @ApiImplicitParam(name = "bannerType", value = "轮播类型(1.新闻，2.首页)",
            required = true, dataType = "String")
    @GetMapping("/getBannerByType")
    public R<List<LawBanner>> getBannerByType(String bannerType) {
        LambdaQueryWrapper<LawBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawBanner::getBannerType, bannerType);
        List<LawBanner> list = bannerService.list(queryWrapper);
        return R.ok(list);
    }

}
